package com.zyk.scaffold.oauth.config.oauth;

import com.zyk.scaffold.core.component.CountRedisLimitComponent;
import com.zyk.scaffold.oauth.component.CaptchaComponent;
import com.zyk.scaffold.oauth.component.TenantCodeCachComponent;
import com.zyk.scaffold.oauth.config.oauth.grant.PasswordTokenGranter;
import com.zyk.scaffold.oauth.config.oauth.grant.RefreshTokenTokenGranter;
import com.zyk.scaffold.oauth.config.oauth.grant.SsoJwtGranter;
import com.zyk.scaffold.oauth.config.oauth.grant.TenantRefreshGranter;
import com.zyk.scaffold.oauth.config.oauth.support.*;
import com.zyk.scaffold.oauth.framework.OauthManager;
import com.zyk.scaffold.oauth.framework.config.properties.OauthProperties;
import com.zyk.scaffold.core.component.PasswordEncoderComponent;
import com.zyk.scaffold.user.rpc.UserRpcService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LocalOauthManagerConfig {

    @Autowired
    private PasswordEncoderComponent passwordEncoderComponent;

    @Autowired
    private CaptchaComponent captchaComponent;

    @Autowired
    private TenantCodeCachComponent tenantCodeCachComponent;

    @DubboReference
    private UserRpcService userRpcService;

    @Bean
    public OauthManager oauthManager(@Autowired LocalAccessTokenConvert localAccessTokenConvert,
                                     @Autowired LocalRefreshTokenConvert LocalRefreshTokenConvert,
                                     LocalClientDetailsService localClientDetailsService,
                                     LocalUserDetailsService localUserDetailsService,
                                     OauthProperties oauthProperties,
                                     CountRedisLimitComponent countRedisLimitComponent){
        OauthManager oauthManager = new OauthManager(oauthProperties);
        oauthManager.registerGrant(new PasswordTokenGranter(localClientDetailsService, localUserDetailsService, passwordEncoderComponent, captchaComponent, tenantCodeCachComponent, countRedisLimitComponent));
        oauthManager.registerGrant(new SsoJwtGranter(localClientDetailsService, localUserDetailsService, userRpcService, tenantCodeCachComponent));
        oauthManager.registerGrant(new RefreshTokenTokenGranter(oauthManager, localUserDetailsService));
        oauthManager.registerGrant(new TenantRefreshGranter(oauthManager, localUserDetailsService));
        oauthManager.getTokenService().registerAccessTokenConvert(localAccessTokenConvert);
        oauthManager.getTokenService().registerRefreshTokenConvert(LocalRefreshTokenConvert);
        oauthManager.getTokenService().setSubjectHandler(new LocalSubjectHandler(oauthManager.getTokenService().getTokenStore()));
        oauthManager.setClientDetailsService(localClientDetailsService);
        oauthManager.setUserDetailsService(localUserDetailsService);
        oauthManager.setLoginCallBcak(new LoginCallBcak(tenantCodeCachComponent));
        return oauthManager;
    }
}
